<template>
    <div v-if="showModal" class="login-expired-overlay" @click="handleConfirm">
        <div class="login-expired-modal" @click.stop>
            <div class="modal-header">
                <div class="warning-icon">⚠️</div>
                <h3 class="modal-title">登录失效</h3>
            </div>
            <div class="modal-body">
                <p class="modal-message">您的登录状态已过期，请重新登录</p>
                <p class="modal-submessage">为了保护您的账户安全，系统会定期要求重新登录</p>
            </div>
            <div class="modal-footer">
                <button class="confirm-btn" @click="handleConfirm">
                    重新登录
                </button>
            </div>
        </div>
    </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { useTokenStore } from '@/stores/token.js'
import { useTeacherStore } from '@/stores/teacher.js'

const router = useRouter()
const tokenStore = useTokenStore()
const teacherStore = useTeacherStore()

const showModal = ref(false)

// 显示登录失效弹窗
const show = () => {
    showModal.value = true
}

// 隐藏弹窗
const hide = () => {
    showModal.value = false
}

// 确认按钮处理
const handleConfirm = () => {
    hide()
    
    // 清除本地存储的token和用户信息
    tokenStore.removeToken()
    teacherStore.clearTeacherInfo()
    
    // 清除localStorage中的数据
    localStorage.removeItem('token')
    localStorage.removeItem('teacherInfo')
    
    // 跳转到登录页
    router.push('/login')
}

// 暴露方法给外部调用
defineExpose({
    show,
    hide
})
</script>

<style scoped>
.login-expired-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.6);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 9999;
    animation: fadeIn 0.3s ease-out;
}

.login-expired-modal {
    background: white;
    border-radius: 12px;
    min-width: 400px;
    max-width: 90%;
    overflow: hidden;
    box-shadow: 0 20px 40px rgba(0, 0, 0, 0.3);
    animation: slideIn 0.3s ease-out;
}

.modal-header {
    padding: 24px 24px 16px 24px;
    text-align: center;
    background: #fff8e1;
    border-bottom: 1px solid #ffe082;
}

.warning-icon {
    font-size: 48px;
    margin-bottom: 12px;
    display: block;
}

.modal-title {
    font-size: 20px;
    font-weight: 600;
    color: #f57c00;
    margin: 0;
}

.modal-body {
    padding: 24px;
    text-align: center;
}

.modal-message {
    font-size: 16px;
    color: #333;
    margin: 0 0 12px 0;
    font-weight: 500;
    line-height: 1.5;
}

.modal-submessage {
    font-size: 14px;
    color: #666;
    margin: 0;
    line-height: 1.4;
}

.modal-footer {
    padding: 16px 24px 24px 24px;
    text-align: center;
}

.confirm-btn {
    background: #FF8C00;
    color: white;
    border: none;
    padding: 12px 32px;
    border-radius: 6px;
    font-size: 16px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.3s ease;
    min-width: 120px;
}

.confirm-btn:hover {
    background: #e67e00;
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(255, 140, 0, 0.3);
}

.confirm-btn:active {
    transform: translateY(0);
}

@keyframes fadeIn {
    from {
        opacity: 0;
    }
    to {
        opacity: 1;
    }
}

@keyframes slideIn {
    from {
        opacity: 0;
        transform: translateY(-50px) scale(0.9);
    }
    to {
        opacity: 1;
        transform: translateY(0) scale(1);
    }
}

/* 响应式设计 */
@media (max-width: 480px) {
    .login-expired-modal {
        min-width: 320px;
        margin: 20px;
    }
    
    .modal-header,
    .modal-body,
    .modal-footer {
        padding-left: 16px;
        padding-right: 16px;
    }
    
    .warning-icon {
        font-size: 40px;
    }
    
    .modal-title {
        font-size: 18px;
    }
    
    .modal-message {
        font-size: 15px;
    }
    
    .modal-submessage {
        font-size: 13px;
    }
}
</style>
